17. JSON Files in Python

JSON Files in Python

JSON Files In Python

Quizzes

Before you download the movie poster images to add to the word cloud as described at the end of the video above, let's first get comfortable with accessing wptools page object attributes. Let's inspect the wptools page object for the E.T. The Extra-Terrestial Wikipedia page . In the Jupyter Notebook below, you will access the images and infobox attributes and the data within them.

Workspace

This section contains either a workspace (it can be a Jupyter Notebook workspace or an online code editor work space, etc.) and it cannot be automatically downloaded to be generated here. Please access the classroom with your account and manually download the workspace to your local machine. Note that for some courses, Udacity upload the workspace files onto https://github.com/udacity , so you may be able to download them there.

Workspace Information:

  • Default file path:
  • Workspace type: jupyter
  • Opened files (when workspace is loaded): n/a

Quiz: JSON Arrays

QUESTION:

Given the wptools page object for the E.T. Wikipedia page (with .get() already called on it), what expression will allow us to access the first image in the images attribute, which is a JSON array?

SOLUTION:

NOTE: The solutions are expressed in RegEx pattern. Udacity uses these patterns to check the given answer

Quiz: JSON Objects

QUESTION:

Given the wptools page object for the E.T. Wikipedia page (with .get() already called on it), what expression will allow us to access the director key of the infobox attribute, which is a JSON object?

SOLUTION:

NOTE: The solutions are expressed in RegEx pattern. Udacity uses these patterns to check the given answer

More JSON in Python

For the example in this lesson, JSON data was sourced from an API. That isn't always the case, though! Sometimes you're given a text file with human readable JSON within it. For this situation, the json library is indispensable. It can parse JSON from strings or files and it can parse JSON into a Python dictionary or list. It can also convert Python dictionaries or lists into JSON strings. The tutorial on the linked documentation page is handy. This Reading and Writing JSON to a File in Python article from Stack Abuse is also great, which outlines json.dump , json.dumps , json.load , and json.loads (four key json library methods) well.

pandas also has JSON functions (the read_json function and the to_json DataFrame method), but the hierarchical advantage of JSON is wasted in pandas' tabular DataFrame so the uses are limited.